package com.itheima.task.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author zhangyan
 * @date 2021/4/5 16:31
 */
public class OSSDemo {

    public static void main(String[] args) throws FileNotFoundException {

        uploadLocalFile();
    }

    //基于文件流上传
    private static void uploadStream() throws FileNotFoundException {
        // 域名
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // AccessKey
        String accessKeyId = "LTAI5tPYHYneC7Vq78TxTjoX";
        // AccessKey Secret
        String accessKeySecret = "LG4MuBFhEtAExWK2RzUed1bR49y8Te";

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上传文件流
        InputStream inputStream = new FileInputStream("C:\\Users\\51032\\Desktop\\AccessKey.txt");
        ossClient.putObject("nineclock374", UUID.randomUUID() + ".txt", inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
    }

    //基于本地文件上传
    private static void uploadLocalFile() {
        // 域名
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // AccessKey
        String accessKeyId = "LTAI5tPYHYneC7Vq78TxTjoX";
        // AccessKey Secret
        String accessKeySecret = "LG4MuBFhEtAExWK2RzUed1bR49y8Te";

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建PutObjectRequest对象
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                "nineclock374",
                       UUID.randomUUID()+".txt",
                            new File("C:\\Users\\51032\\Desktop\\AccessKey.txt"));

        // 上传文件
        ossClient.putObject(putObjectRequest);

        // 关闭OSSClient
        ossClient.shutdown();
    }
}
